var app = angular.module('app',[]).controller('gwc',
		function($scope,$http,queue){
        
        
        var getSessionUser = function(){ 
 		   $http.post("../pub/getSessionUser").success(function(data){
 			   $scope.username = data.username;
 		   })
 		} 
	 	getSessionUser();
	 	$scope.logout = function(){
	     	$http.post("../pub/logout1").success(function(data) {
	     		if(data.status == "ok"){
	 				window.open("../pub/userlogin");
	 			}
	     	})
	 	}
	 	$scope.money =0;
	 	var prices = 0;
	 	$scope.gwc = [];
	 	$scope.isTrue = false;
        var findAllGwcByUsername = function() {
        	$http.post("./findAllGwcByUsername").success(function(data){
        		
        			for(var i=0,j=data.length;i<j;i++){
        				var items = data[i];
        				items.bol=false;
        				prices += parseInt(data[i].num*data[i].price);
        				$scope.gwc.push(items);
        			}
        			
        		window.location.reload();
        		
        		
        	})
        }
        findAllGwcByUsername();
        
        $scope.subNum = function(xm,num) {
        	var carMsg = {
        			"xm":xm
        	};
        	if(num>1){
        		$http.post("./subNum",carMsg).success(function(data){
        			findAllGwcByUsername();
        		})
        	}else if(num == 1){
        		if(confirm('确定要删除该辆车?')){
        		$http.post("./deleteNum",carMsg).success(function(data){
        			findAllGwcByUsername();
        		})
        	}
        	}
        }
        
        
        $scope.addNum = function(xm,num) {
        	var carMsg = {
        			"xm":xm
        	};
        	if(num<5){
        		$http.post("./addNum",carMsg).success(function(data){
        			findAllGwcByUsername();
        		})
        	}else if(num == 5){
        		alert("每人每辆最多租5辆！");
        	}
        }
        
        $scope.deleteCar = function(xm,num) {
        	var carMsg = {
        			"xm":xm
        	};
        	if(confirm('确定要删除该车?')){
        		
        		$http.post("./deleteNum",carMsg).success(function(data) {
        			findAllGwcByUsername();
        		})
        	}
        	}
      
        $scope.selectCar = function(price,num,bol) {
        	for(var i=0,j=$scope.gwc.length;i<j;i++){
        		
        		if($scope.gwc[i].bol == true) {
        			$scope.isTrue = false;
        		}
        	}

        	if(bol == false){
        		$scope.money += price*num;
        	}else{
        		$scope.money -= price*num;
        	}
        	
        }
        
        $scope.selectAll = function(q) {
        	
        		for(var i=0,j=$scope.gwc.length;i<j;i++){
        			$scope.gwc[i].bol = !q;
        			$scope.money = prices;
        			if(q == true){
        				$scope.money = 0;
        			}
        	}
        		
        }
       
        
})